<?php

namespace App\Cms\Controller;

class Weixin extends \Controller {

    public function api() {

        $wxapi = new \Wxapi();
        
        if (!$this->request->isPost()) {
            die($wxapi->checkSignature($this->request->getGet()));
        }
        
        $postStr = file_get_contents('php://input');
        if (!empty($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes') {
            $postStr = $wxapi->decryptMsg($_GET['msg_signature'], $_GET['timestamp'], $_GET['nonce'], $postStr);
        }
        
        $message = $wxapi->parse($postStr);
        
        $openid = $message['from'];
        
        $sessionid = md5($message['from'] . $message['to']);
        session_id($sessionid);
        session_start();
        
        $_SESSION['openid'] = $openid;

        $resp = ''; // 响应消息
        
        if ($message['type'] == 'text') {
            switch ($message['content']) {
                case '回复关键字':
                    $mid = 'bRD0InPat3T5KSXy2VvBx1huvQ5wYfQ45MtGaajB5sY';
                    $resp = $wxapi->respImage($message['to'], $message['from'], $mid);
                    break;
                default:
                    $msg = "默认消息";
                    $resp = $wxapi->respText($message['to'], $message['from'], $msg);
                    break;
            }
        }

        if ($message['type'] == 'event') {
            switch ($message['event']) {
                case 'subscribe':
                    $msg = "您好，欢迎关注本平台！";
                    $resp = $wxapi->respText($message['to'], $message['from'], $msg);
                    break;
                case 'unsubscribe':

                    break;
                case 'scan':
                    $qrcode = $wxapi->Qrcode();
                    $resp = $qrcode->response($message);
                    break;
                case 'click':
                    if ($message['eventkey'] == 'KEFU_HOT_LINE') {
                        $resp = $wxapi->respText($message['to'], $message['from'], '4009600636');
                    }
                    break;
                default:
                    $resp = $wxapi->respText($message['to'], $message['from'], '消息错误！');
                    break;
            }
        }

        
        if (!empty($_GET['encrypt_type']) && $_GET['encrypt_type'] == 'aes') {
            $resp = $wxapi->encryptMsg($resp, $_GET['timestamp'], $_GET['nonce']);
        }
        
        die($resp);
        
    }
    
    public function add_material() {
        $jpg = ROOT_PATH . '/uploads/qun1.jpg';
        $wxapi = new \Wxapi();
        if ($result = $wxapi->Material()->uploadMaterialImage($jpg)) {
            print_r($result);
        } else {
            echo 'material upload failed';
        }
        die();
    }

    public function qrcode() {
        $wxapi = new \Wxapi();
        $qrcode = $wxapi->Qrcode();

        $qrid = $qrcode->createTempQr('HUODONG', 'HUODONG');
        $qr = $qrcode->getQRUrlById($qrid);
        echo "<img src='$qr' />";
        die();
    }
    
    public function jsconfig() {
        $url = $this->request->get('url');
        $wxapi = new \Wxapi();
        $this->json(1, $wxapi->getJsConfig($url));
    }

}
